package com.wondertek.web.controller.sys;

import com.wondertek.common.base.Result;

import com.wondertek.common.log.AutoLog;

import com.wondertek.common.util.security.PreAuthorize;
import com.wondertek.service.dto.sys.req.BaseUpdateReq;
import com.wondertek.service.dto.sys.req.SysDepartAddReq;
import com.wondertek.service.dto.sys.req.SysDepartQueryReq;
import com.wondertek.service.dto.sys.req.SysDepartUpdateReq;
import com.wondertek.service.entity.sys.SysDepartPo;
import com.wondertek.service.service.sys.ISysDepartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @author xqs
 */
@RestController
@RequestMapping("/backend/sys/depart")
@Tag(name = "系统部门管理")
public class SysDepartController {

    @Resource
    private ISysDepartService sysDepartService;

    /**
     * 查询所有部门
     * @return /
     */
    @GetMapping("/findList")
    @Operation(summary = "查询所有部门")
    public Result<List<SysDepartPo>> findList(SysDepartQueryReq sysDepartQueryReq){
        return Result.success(sysDepartService.findList(sysDepartQueryReq));
    }

    /**
     * 查询所有部门 (树形结构)
     * @return /
     */
    @GetMapping("/findTree")
    @Operation(summary = "查询所有部门(树形结构)")
    @PreAuthorize("@el.check('backend:sys:user:page','backend:sys:depart:findTree')")
    public Result<List<SysDepartPo>> findTree(SysDepartQueryReq sysDepartQueryReq){
        return Result.success(sysDepartService.findTree(sysDepartQueryReq));
    }

    /**
     * 用户id查询部门
     * @return /
     */
    @GetMapping("/findListByUserId")
    @Operation(summary = "用户id查询部门")
    public Result<List<SysDepartPo>> findListByUserId(@RequestParam("userId") String userId){
        return Result.success(sysDepartService.findListByUserId(userId));
    }

    /**
     * 新增部门
     * @param sysDepartAddReq /
     * @return /
     */
    @PostMapping("/add")
    @Operation(summary = "新增部门")
    @AutoLog(category = "sys", handleType = "log.department.insert",
            handleNote = "'新增部门:'+#sysDepartAddReq.departName")
    @PreAuthorize("@el.check('backend:sys:depart:add')")
    public Result<?> add(@Validated @RequestBody SysDepartAddReq sysDepartAddReq){
        sysDepartService.add(sysDepartAddReq);
        return Result.success();
    }

    /**
     * 上级同部门
     * @param id /
     * @return /
     */
    @Operation(summary = "查询上级同部门")
    @PostMapping("/findParentTypeList")
    public Result<List<SysDepartPo>> findParentList(@RequestParam("id") String id){
        List<SysDepartPo> list = sysDepartService.findParentTypeList(id);
        return Result.success(list);
    }

    /**
     * 修改部门
     * @param sysDepartUpdateReq /
     * @return /
     */
    @PostMapping("/update")
    @Operation(summary = "修改部门")
    @AutoLog(category = "sys", handleType = "log.department.update",
            handleNote = "'修改部门:'+#sysDepartUpdateReq.departName")
    @PreAuthorize("@el.check('backend:sys:depart:update')")
    public Result<?> update(@Validated @RequestBody SysDepartUpdateReq sysDepartUpdateReq){
        sysDepartService.update(sysDepartUpdateReq);
        return Result.success();
    }

    /**
     * 删除部门
     * @param baseUpdateReq /
     * @return /
     */
    @PostMapping("/delete")
    @Operation(summary ="删除部门")
    @AutoLog(category = "sys", handleType = "log.department.delete",
            handleNote = "'删除部门,id:'+#baseUpdateReq.id")
    @PreAuthorize("@el.check('backend:sys:depart:delete')")
    public Result<?>  delete(@RequestBody @Validated BaseUpdateReq baseUpdateReq ){
        sysDepartService.delete(baseUpdateReq.getId());
        return  Result.success();
    }

}
